//
//  CollectItemCollectionViewCell.swift
//  LeTu
//
//  Created by gy on 2023/12/27.
//

import UIKit

class CollectItemCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    static func getFixCellHeight() -> CGFloat {
        return 8 + (12+20) + (10+12*kAppThemeManager.fontScale+2) + (10+12*kAppThemeManager.fontScale+2)
    }
    
    func setCollectModel(_ collectModel: CollectModel) {
        
        // 设置景区等级
        setScenicLevel("\(collectModel.scenicLevel)")
        
        // 设置景区名称
        setName(LanguageManager.shared.currentLanguage == .zh ? collectModel.name : collectModel.englishName)
        
        // 设置景区讲解播放个数
        setStoryNumCount("\(collectModel.storyNum)")
        
        // 设置景区点赞数
        setZanCount("\(collectModel.collections)", hasCollected: collectModel.hasCollected ? 1:0)

        // 设置景区收听数
        setListenersCount("\(collectModel.listeners)")
        
        // 设置景区图片
        setScenicPic(collectModel.picUrl)
        
    }
    func setScenicModel(_ scenicModel: ScenicModel) {
        
        // 设置景区等级
        setScenicLevel("\(scenicModel.scenicLevel)")
        
        // 设置景区名称
        setName(LanguageManager.shared.currentLanguage == .en ? scenicModel.englishName:scenicModel.name)
        
        // 设置景区讲解播放个数
        setStoryNumCount("\(scenicModel.storyNum)")
        
        // 设置景区点赞数
        setZanCount("\(scenicModel.collections)", hasCollected: scenicModel.hasCollected ? 1:0)

        // 设置景区收听数
        setListenersCount("\(scenicModel.listeners)")
        
        // 设置景区图片
        setScenicPic(scenicModel.picUrl)
        
    }
    // 设置景区等级
    private func setScenicLevel(_ scenicLevel: String) {
        
        let scenicLevelText = "\(scenicLevel)A"
        scenicLevelLabel.text = scenicLevelText
        scenicLevelLabel.snp.updateConstraints { make in
            make.width.equalTo(scenicLevelText.widthWithLimit(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 14), font: kSetPingFangRegular(11))+10)
        }
        
    }
    // 设置景区照片
    private func setScenicPic(_ scenicPic: String) {
        
        var imgH = 0.0
        
        if let url = URL.encodedURL(string: scenicPic) {
            imgH += XHWebImageAutoSize.imageHeight(for: url, layoutWidth: self.width, estimateHeight: imgH)
        }
        imgView.sd_setImage(with: URL.encodedURL(string: scenicPic), placeholderImage: UIImage(named: "pic_placeholder"))
        imgView.snp.updateConstraints { make in
            make.height.equalTo(imgH)
        }
        
    }
    // 设置景区名称
    private func setName(_ name: String) {
        nameLabel.text = name
    }
    // 设置景区讲解播放个数
    private func setStoryNumCount(_ storyNumCount: String) {
        storyNumCountLabel.attributed.text = "\(.image(UIImage(named: "play_icon")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 13, height: 13)))) \(storyNumCount)\("处讲解".localized)"
    }
    // 设置景区点赞数
    private func setZanCount(_ zanCount: String, hasCollected: Int) {
        zanLabel.attributed.text = "\(.image(UIImage(named: hasCollected == 1 ? "collect_zan" : "collect_unzan")!, .custom(.offset(CGPoint(x: 0, y: -1)), size: CGSize(width: 14, height: 14)))) \(zanCount)"
        zanLabel.snp.updateConstraints { make in
            make.width.equalTo(zanLabel.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 16)))
        }
    }
    // 设置景区收听数
    private func setListenersCount(_ listenersCount: String) {
        listenersCountLabel.attributed.text = "\(.image(UIImage(named: "listeners_count_black_icon")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 13, height: 13)))) \(listenersCount)"+"人已听".localized
    }
    
    // MARK: - UIButtonAction
    @objc private func zanLabelGrAction() { }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(imgView)
        contentView.addSubview(scenicContentView)
        scenicContentView.addSubview(scenicLevelLabel)
        scenicContentView.addSubview(nameLabel)
        scenicContentView.addSubview(zanLabel)
        contentView.addSubview(storyNumCountLabel)
        contentView.addSubview(listenersCountLabel)

        imgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(0)
        }
        listenersCountLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-10)
            make.left.equalTo(6)
            make.right.equalTo(-6)
            make.height.equalTo(12*kAppThemeManager.fontScale+2)
        }
        storyNumCountLabel.snp.makeConstraints { make in
            make.left.right.equalTo(listenersCountLabel)
            make.bottom.equalTo(listenersCountLabel.snp.top).offset(-10)
            make.height.equalTo(12*kAppThemeManager.fontScale+2)
        }
        scenicContentView.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.bottom.equalTo(storyNumCountLabel.snp.top).offset(-10)
            make.left.right.equalTo(listenersCountLabel)
        }
        scenicLevelLabel.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
            make.width.equalTo(0)
            make.height.equalTo(14)
        }
        zanLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(0)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(scenicLevelLabel.snp.right).offset(6)
            make.centerY.equalToSuperview()
            make.right.lessThanOrEqualTo(zanLabel.snp.left).offset(-6)
        }
        
        let zanTapGR = UITapGestureRecognizer(target: self, action: #selector(zanLabelGrAction))
        zanLabel.addGestureRecognizer(zanTapGR)
        
    }
    private(set) lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 12
        imgView.layer.masksToBounds = true
        imgView.backgroundColor = .app_white
        return imgView
    }()
    private lazy var scenicContentView = UIView()
    private lazy var scenicLevelLabel: UILabel = {
        let scenicLevelLabel = UILabel()
        scenicLevelLabel.textColor = .app_white
        scenicLevelLabel.font = kSetPingFangRegular(11)
        scenicLevelLabel.textAlignment = .center
        scenicLevelLabel.backgroundColor = UIColor.hex(hexString: "#9C4615")
        scenicLevelLabel.layer.cornerRadius = 7
        scenicLevelLabel.layer.masksToBounds = true
        return scenicLevelLabel
    }()
    private var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = kSetPingFangRegular(15)
        return nameLabel
    }()
    private var zanLabel: UILabel = {
        let zanLabel = UILabel()
        zanLabel.font = kSetPingFangRegular(12)
        zanLabel.textAlignment = .right
        return zanLabel
    }()
    private var storyNumCountLabel: UILabel = {
        let storyNumCountLabel = UILabel()
        storyNumCountLabel.font = kSetPingFangRegular(11)
        return storyNumCountLabel
    }()
    private var listenersCountLabel: UILabel = {
        let listenersCountLabel = UILabel()
        listenersCountLabel.font = kSetPingFangRegular(12)
        listenersCountLabel.textColor = UIColor(hex: 0xA1A1A1)
        return listenersCountLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
